package com.triones.api.controller.api;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.triones.api.entity.CryptoWalletDO;
import com.triones.api.entity.LeptageQuoteDO;
import com.triones.api.entity.WithdrawCryptoRequstBodyBO;
import com.triones.api.entity.bo.*;
import com.triones.api.entity.param.CryptoParams;
import com.triones.api.entity.vo.*;
import com.triones.api.service.CryptoWalletService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * api - 加密货币相关接口
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/crypto")
public class ApiCryptoController extends BaseController {

    private final CryptoWalletService cryptoWalletService;

    /**
     * 加密货币钱包列表
     */
    @GetMapping(value = "/wallet/list")
    public R<List<CryptoWalletVO>> apiWalletList() {
        return R.ok(cryptoWalletService.apiWalletList());
    }

    /**
     * 加密货币交易列表（分页）
     * transferType: ( transfer-out:转出、transfer-in:收入 )
     */
    @GetMapping(value = "/trade/pageList")
    public TableDataInfo<CryptoBillVO> apiTradePageList(CryptoParams params, PageQuery pageQuery) {
        return cryptoWalletService.apiTradePageList(params,pageQuery);
    }

    /**
     * 提现
     * @param bo
     * @return
     */
    @PostMapping("/exchange")
    public R<Void> exchange(@Validated @RequestBody ExchangeBO bo) {
        cryptoWalletService.exchange(bo);
        return R.ok("提现交易已发送");
    }

    /**
     * 加密货币钱包详情
     * @return
     */
    @GetMapping("/walletDetail")
    public R<CryptoWalletVO> walletDetail(@NotBlank(message = "币种不能为空") String ccy,@NotBlank(message = "公链不能为空") String chain) {
        return R.ok(cryptoWalletService.walletDetail(ccy,chain));
    }

    /**
     * 提币
     * @param bo
     * @return
     */
    @PostMapping("/crypto")
    public R<Void> crypto(@RequestBody CryptoBO bo) {
        cryptoWalletService.crypto(bo);
        return R.ok("提币交易已发送");
    }

    /**
     * 查询可用转账地址
     * @return
     */
    @GetMapping("/commonUseAddressList")
    public R<List<CryptoInCommonUseAddressVO>> inCommonUseAddressPageList(@NotBlank(message = "币种不能为空") String ccy,
                                                                                @NotBlank(message = "公链不能为空") String chain,String keyword) {
        return R.ok(cryptoWalletService.inCommonUseAddressPageList(ccy,chain,keyword));
    }

    /**
     * 充值测试
     */
    @GetMapping(value = "/mock/deposit/crypto")
    @SaIgnore
    public R<Boolean> mockDepositCrypto(CryptoWalletDO dto) {
        return R.ok(cryptoWalletService.mockDepositCrypto(dto));
    }

    /**
     * 查询钱包余额
     */
    @GetMapping(value = "/mock/balance")
    @SaIgnore
    public R<List<Map>> mockBalance(String accountId) {
        return R.ok(cryptoWalletService.mockBalance(accountId));
    }

    /**
     * 加密货币交易详情
     */
    @GetMapping(value = "/trade/detail/{id}")
    public R<CryptoBillVO> apiTradeDetail(@NotNull(message = "交易id不能为空") @PathVariable Long id) {
        return R.ok(cryptoWalletService.apiTradeDetail(id));
    }

    /**
     * 模拟提币交易结果
     */
    @GetMapping(value = "/mock/withdraw/crypto")
    @SaIgnore
    public R<Boolean> mockWithdrawCrypto(WithdrawCryptoRequstBodyBO bo) {
        return R.ok(cryptoWalletService.mockWithdrawCrypto(bo));
    }

    /**
     * 模拟提币交易结果
     */
    @GetMapping(value = "/mock/transfer")
    @SaIgnore
    public R<Boolean> transfer(TransferRequestBodyBO bodyBO) {
        String direction = "IN";
        Long uid = 10002L;
        return R.ok(cryptoWalletService.transfer(bodyBO,direction,uid));
    }

    /**
     * 根据币种查询公链列表
     */
    @GetMapping(value = "/chain/list")
    public R<List<CryptoWalletVO>> apiChainWalletList(@NotBlank(message = "币种不能为空") String ccy) {
        return R.ok(cryptoWalletService.apiChainWalletList(ccy));
    }

    /**
     * leptage询价
     * @param bo
     * @return
     */
    @PostMapping("/leptage/quote")
    public R<LeptageQuoteDO> leptageQuote(@Validated @RequestBody LeptageQuoteBO bo) {
        bo.setQuoteObj("member");
        return R.ok(cryptoWalletService.leptageQuote(bo));
    }

    /**
     * 模拟提币交易结果
     */
    @GetMapping(value = "/mock/totalBalance")
    @SaIgnore
    public R<List<Map>> totalBalance(String ccy) {
        return R.ok(cryptoWalletService.totalBalance(ccy));
    }

    /**
     * 换汇
     */
    @GetMapping(value = "/mock/exchange")
    @SaIgnore
    public R<Map> totalExchange(String ccyPair,String sellAmount) {
        return R.ok(cryptoWalletService.totalExchange(ccyPair,sellAmount));
    }

    /**
     * 获取提币手续费
     */
    @GetMapping(value = "/cryptoWithdrawFee")
    public R<String> getCryptoWithdrawFee(@NotBlank(message = "链不能为空") String chain) {
        return R.ok("操作成功！",cryptoWalletService.getCryptoWithdrawFee(chain));
    }
}
